package com.example.demo;

import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class YaoPinLIeBiaoController {


    @FXML
    AnchorPane pane;

    private static List<Dept> DEPTS = new ArrayList<>();

    static {
        DEPTS.add(new Dept() );
        DEPTS.add(new Dept(20 ,"aaa" ,"aaaaaa") );
        DEPTS.add(new Dept(30 ,"aaabbb" ,"bbbbbb") );
        DEPTS.add(new Dept(40 ,"aaaccc" ,"ccccccc") );
        DEPTS.add(new Dept(50 ,"ccddd" ,"dddddd") );
        DEPTS.add(new Dept(60 ,"accaa" ,"aaaaaa") );
        DEPTS.add(new Dept(70 ,"bcbb" ,"bbbbbb") );
        DEPTS.add(new Dept(80 ,"ccddc" ,"ccccccc") );
        DEPTS.add(new Dept(90 ,"dddcc" ,"dddddd") );
    }
    @FXML
    private TableView<Dept> tv_dept;
    @FXML
    private TableColumn<Dept,Integer> tc_deptno;
    @FXML
    private TableColumn<Dept,String> tc_dname;
    @FXML
    private TableColumn<Dept,String> tc_loc;

    @FXML
    private TextField  tf_dname;

    private StringProperty dnameProperty;

    @FXML
    public void initialize() {
        tc_deptno.setCellValueFactory( new PropertyValueFactory<>("deptno") );
        tc_dname.setCellValueFactory( new PropertyValueFactory<>("dname"));
        tc_loc.setCellValueFactory( new PropertyValueFactory<>("loc"));
        tv_dept.setItems( FXCollections.observableList(DEPTS)  );
        dnameProperty =  tf_dname.textProperty();
        dnameProperty.addListener((observable, oldValue, newValue) -> {
            List<Dept> lds  = DEPTS.stream().filter(dept -> dept.getDname().contains(newValue)).toList();
            tv_dept.setItems( FXCollections.observableList(lds)  );
        });
    }
    @FXML

    public void tf_dnameKeyPressed(KeyEvent event) {
        if(event.getCode() == KeyCode.DOWN) {
            int  i  =  tv_dept.getSelectionModel().getSelectedIndex() ;
            if( i < 0 ) {
                tv_dept.requestFocus();
                tv_dept.getSelectionModel().select(0);
                tv_dept.getFocusModel().focus(0);
            }else{
                tv_dept.getSelectionModel().select(i+1);
                tv_dept.getFocusModel().focus(i+1);
            }
        }
    }

    /**
     *
     * @param event
     */
    @FXML
    public void   tb_deptKeyPressed(KeyEvent event){
        if (event.getCode() == KeyCode.ENTER){
            Dept dept = tv_dept.getSelectionModel().getSelectedItem();
            if(dept != null){
                System.out.println(dept.getDeptno() + " " + dept.getDname() + " " + dept.getLoc());
            }

            HelloController.welcomeTextStringProperty.setValue( dept.toString() );


            ModalStage.modalStage.close();
        }
    }
}
